﻿using Delonix_Regia_HMS.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Delonix_Regia_HMS
{
    public partial class UserAccounts : Form
    {
        public UserAccounts()
        {
            InitializeComponent();
        }

        //The textbox changes information with a different selection on the listView
        private void listViewStaffManagement_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewStaffManagement.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in listViewStaffManagement.SelectedItems)
                {
                    tbxModifyUserAccountStaffID.Text = listViewStaffManagement.SelectedItems[0].Text;
                    tbxModifyUserAccountPassword.Text = listViewStaffManagement.SelectedItems[0].SubItems[1].Text;
                    tbxModifyUserAccountStaffName.Text = listViewStaffManagement.SelectedItems[0].SubItems[2].Text;
                    cbxStaffAccountManagementStaffType.Text = listViewStaffManagement.SelectedItems[0].SubItems[3].Text;
                }
            }
        }

        //Retrieves the staff by the different staff type
        private void cbxSearchByType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxSearchByType.SelectedItem.ToString() == "Administrator")
            {
                DBManager.ListViewStaffAccountManagementAdministrator(listViewStaffManagement);
            }
            else if (cbxSearchByType.SelectedItem.ToString() == "Manager")
            {
                DBManager.ListViewStaffAccountManagementManager(listViewStaffManagement);
            }
            else if (cbxSearchByType.SelectedItem.ToString() == "Receptionist")
            {
                DBManager.ListViewStaffAccountManagementReceptionist(listViewStaffManagement);
            }
            else if (cbxSearchByType.SelectedItem.ToString() == "Housekeeper")
            {
                DBManager.ListViewStaffAccountManagementHousekeeper(listViewStaffManagement);
            }
        }

        
        //Delete User Account
        private void btnDeleteUserAccount_Click(object sender, EventArgs e)
        {
            int username = Convert.ToInt32(tbxModifyUserAccountStaffID.Text);
            DBManager.DeleteStaffAccount(username);
            MessageBox.Show("Staff Account Deleted");
            ClearTextBoxes(this.Controls);
            ClearComboBoxes(this.Controls);
        }

        //Modify User Account
        private void btnModifyUserAccount_Click(object sender, EventArgs e)
        {
            StaffAccount sa = new StaffAccount();
            Staff s = new Staff();
            sa.Username = Convert.ToInt32(tbxModifyUserAccountStaffID.Text);
            sa.Password = tbxModifyUserAccountPassword.Text;
            s.StaffType = cbxStaffAccountManagementStaffType.SelectedItem.ToString();
            DBManager.ModifyStaffAccount(sa,s);
            MessageBox.Show("Staff Account Updated");
            ClearTextBoxes(this.Controls);
            ClearComboBoxes(this.Controls);
        }

        //Create User Account
        private void btnCreateUserAccount_Click(object sender, EventArgs e)
        {
            CreateAccount ca = new CreateAccount();
            ca.ShowDialog();
        }

        //Clear textboxes after modify/update
        private void ClearTextBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                TextBox tb = ctrl as TextBox;
                if (tb != null)
                {
                    tb.Text = "";
                }

                else
                {
                    ClearTextBoxes(ctrl.Controls);
                }
            }
        }

        //Clear ComboBoxes after modify/update
        private void ClearComboBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                ComboBox cb = ctrl as ComboBox;
                if (cb != null)
                {
                    cb.Text = "";
                }

                else
                {
                    ClearComboBoxes(ctrl.Controls);
                }
            }
        }

        
    }
}
